package com.leetcode;

import org.junit.Test;

/**
 * Created by Z-hong on 2022/10/1.
 */

public class LeetCode1694 {
    public String reformatNumber(String number) {
        char[] chars = new char[100];
        int count = 0;
        for (int i = 0; i < number.length(); i++) {
            if (number.charAt(i) == ' ' || number.charAt(i) == '-') {
                continue;
            }
            chars[count++] = number.charAt(i);
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < count; i++) {
            sb.append(chars[i]);
            if (i % 3 == 2 && count - 1 != i) {
                sb.append('-');
            }
        }
        if (count % 3 == 1) {
            sb.deleteCharAt(sb.length() - 2);
            sb.insert(sb.length() - 2, '-');
        }
        return sb.toString();
    }

    @Test
    public void test() {
        String s = "1-23-45 62";
        System.out.println(reformatNumber(s));
    }
}
